﻿using DemountCIM.Framework.DataBase.Table;
using SqlSugar;
using System.Security.Cryptography;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using WFDataBase;

namespace DemountCIM.Framework.DataBase
{
    public class UserRepository : BaseRepository<TbUser>, IUserRepository
    {
        private TbUser _curUser;    // 当前用户

        public UserRepository(SqlSugarScope context) : base(context)
        {
            _curUser = new TbUser();
        }

        /// <summary>
        /// 获取当前用户等级
        /// </summary>
        /// <returns></returns>
        public UserLevel GetUserLevel()
        {
            return _curUser.Level;
        }

        /// <summary>
        /// 获取当前用户信息
        /// </summary>
        /// <param name="curUser"></param>
        /// <returns></returns>
        public bool GetCurUser(ref TbUser curUser)
        {
            if (null != curUser)
            {
                curUser.Id = this._curUser.Id;
                curUser.Name = this._curUser.Name;
                curUser.Role = this._curUser.Role;
                curUser.Level = this._curUser.Level;
                curUser.Pwd = this._curUser.Pwd;

            }

            return true;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="name"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool UserLogin(string name, string password)
        {
            password = MD5Encrypt16(password);

            return GetAppointUser(name, password);
        }

        /// <summary>
        /// 用户登出
        /// </summary>
        public bool UserLogout()
        {
            _curUser.Id = -1;
            _curUser.Level = UserLevel.无效;
            _curUser.Name = string.Empty;
            _curUser.Pwd = string.Empty;

            return true;
        }

        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool AddUserInfo(TbUser user)
        {
            user.Pwd = MD5Encrypt16(user.Pwd);
            return Insert(user);
        }

        /// <summary>
        /// 查询用户是否存在
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool SelectUserExists(string name)
        {
            return Context.Queryable<TbUser>().Count(x => x.Name == name) > 0;
        }

        /// <summary>
        /// 删除用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool DeleteUserInfo(TbUser user)
        {
            return Delete(user);
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool ModifyUserInfo(TbUser user)
        {
            user.Pwd = MD5Encrypt16(user.Pwd);
            return Update(user);
        }

        /// <summary>
        /// 获取指定用户
        /// </summary>
        /// <param name="name"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool GetAppointUser(string name, string password)
        {
            var user = GetFirst(a => a.Name == name && a.Pwd == password);
            if (user == null)
                return false;
            _curUser.Id = user.Id;
            _curUser.Level = user.Level;
            _curUser.Name = user.Name;
            _curUser.Pwd = user.Pwd;
            return true;
        }

        /// <summary>
        /// 16位MD5加密
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        private string MD5Encrypt16(string password)
        {
            var md5 = new MD5CryptoServiceProvider();
            string strPassword = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(password)), 4, 8);
            strPassword = strPassword.Replace("-", "");
            return strPassword;
        }
    }
}
